<template>
  <div class="topbar">
    <div class="wrap topbar-wrap">
      <!-- 顶部左侧 -->
      <div class="top-left">欢迎来到叩丁狼积分商城</div>
      <!-- 顶部右侧 -->
      <ul class="top-right">
        <li>
          <img :src="userInfo.headImg" width="24px" alt="" />
          用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="login-btn cart" v-if="isLogined">
          <span class="img"></span> 购物车<span class="rs">{{
            cartTotal
          }}</span>
        </li>
        <li class="login-btn" v-else @click="changeIsShowLoginModal">登录</li>
      </ul>
    </div>
  </div>
</template>
<script>
import { weixinLoginAPI } from "@/request/api";
import { mapMutations, mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      token: "",
    };
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      cartTotal: (state) => state.userInfo.cartTotal,
      userInfo: (state) => state.userInfo.userInfo,
    }),
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLoginedShow: "loginStatus/changeIsLoginedShow",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncchangeIsShowToast: "showToast/asyncchangeIsShowToast",
      asyncchangeUserInfo: "userInfo/asyncchangeUserInfo",
    }),
  },
  // 发送微信请求
  async created() {
    this.token = localStorage.getItem("x-auth-token");
    // console.log("进来了topbar  created钩子函数");
    let code = this.$route.query.code;
    // console.log("code", code);
    if (code) {
      let res = await weixinLoginAPI({
        code,
      });
      // console.log("微信登录res", res);
      // res三种情况
      if (res.code == 0) {
        // 1.code==0 登录成功
        // 1.1提示登录成功
        this.asyncchangeIsShowToast({
          isShow: true,
          msg: "扫码登录成功",
          type: "success",
        });
        // 1.2.存token
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        // 1.3.更新右上角购物车按钮显示
        this.changeIsLoginedShow(true);
        // 1.4.清除地址栏中的code
        this.$router.push(this.$route.path);
        // 更新用户信息
        this.asyncchangeUserInfo();
      } else if (res.code == 400) {
        // 2.code==400 code失效 重新扫码
        this.changeIsShowLoginModal(true);
        // alert("请重新扫码");
        this.asyncchangeIsShowToast({
          isShow: true,
          msg: "请重新扫码",
          type: "danger",
        });
      } else if (res.code == 407) {
        // 3.code==407 需要用户再进行一次手机号方式登录 (后期让后台将微信和手机号进行关联)
        // 3.1提示用户需要进行一次手机号登录
        // alert("请使用手机号再进行登录");
        this.asyncchangeIsShowToast({
          isShow: true,
          msg: "请使用手机号再进行登录",
          type: "danger",
        });
        // 3.2 自动打开登录模块
        this.changeIsShowLoginModal(true);
        // 3.3 存储uuid（一会登录要用）
        localStorage.setItem("uuid", res.uuid);
      }
    } else {
      let token = localStorage.getItem("x-auth-token");
      this.changeIsLoginedShow(Boolean(token));
      if (token) {
        // 如果有登录则更新用户信息
        this.asyncchangeUserInfo();
      } else {
        this.initUserInfo();
      }
    }
  },
  // 路由监听地址的变化
  // 解决:登录成功后,手动删除token.切换导航时,购物车没变,方式一
  // watch: {
  //   "$route.path": {
  //     handler(newVal, oldVal) {
  //       console.log("watch...", newVal, oldVal);
  //       if (newVal != oldVal) {
  //         // 判断当前是否有token
  //         let token = localStorage.getItem("token");
  //         this.changeIsLoginedShow(Boolean(token));
  //       }
  //     },
  //   },
  // },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  background: #242b39;
  color: #fffefe;
  line-height: 40px;
  font-size: 14px;
  font-family: SourceHanSansSC;
  .topbar-wrap {
    display: flex;
    justify-content: space-between;
  }
  .top-right {
    width: 495px;
    display: flex;
    justify-content: space-between;

    li {
      text-align: center;
      cursor: pointer;
      &.login-btn {
        height: 40px;
        width: 124px;
        background: #0a328e;
      }
      img {
        vertical-align: middle;
        border-radius: 50%;
      }
    }
    .cart {
      display: flex;
      .img {
        margin: 10px 10px 10px 15px;
        display: block;
        width: 20px;
        height: 19px;
        background: url("../assets/img/cart.png");
      }
      .rs {
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background-color: red;
        line-height: 20px;
        text-align: center;
        margin-top: 10px;
        margin-left: 10px;
      }
    }
  }
}
</style>